package com.museum.base;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.museum.constants.Constants;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    // 添加填充
    @Override
    public void insertFill(MetaObject metaObject) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = dateFormat.format(new Date());
        this.setFieldValByName("createTime", date, metaObject);
        this.setFieldValByName("updateTime", date, metaObject);
        this.setFieldValByName("isAvailability", 0, metaObject);
        this.setFieldValByName("isDeleted", 0, metaObject);
        this.setFieldValByName("statusId", 1, metaObject);
        this.setFieldValByName("version", 1, metaObject);
        this.setFieldValByName("loginType", Constants.MEMBER_LOGIN_TYPE_PC,metaObject);
    }

    //修改填充
    @Override
    public void updateFill(MetaObject metaObject) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        this.setFieldValByName("updateTime", dateFormat.format(new Date()), metaObject);
    }
}
